
package com.example.swenp;


import javax.net.ssl.SSLSocketFactory;


import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class Startseite extends Activity {
	ProgressDialog ladekreis;
	FlipAnimation animation;
	EditText ed;
	ViewFlipper flipper;
	int gewaehlt;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startseite);
        
        
        animation = new FlipAnimation();
        gewaehlt = 0;
        
        flipper = (ViewFlipper) findViewById(R.id.startseite_flipper);
        
        auflisten("");
        
	    ed = (EditText)findViewById(R.id.ed_startseite_chat_suchen);
        ed.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
            	auflisten(ed.getText().toString());
            	
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        }); 
     

		// Service starten
        startService(new Intent(this, Service.class));
    }
    
    public void suche_loeschen(final View view){
    	ed.setText("");
    }
    public void auflisten(String _wen){
    	String wen = _wen;
    
    	LinearLayout ll = (LinearLayout) findViewById(R.id.freunde_platzhalter);
	  	LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

	  	ll.removeAllViews();
	  	

	  	if(wen.equalsIgnoreCase("")){
	  	int i = 3;
	  	while(i>1){
	  		View vv = inflater.inflate(R.layout.chatleiste, null);
	  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_chatleiste_profilbild);
	  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_chatleiste_benutzername);
	  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_chatleiste_online_offline);
	  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_chatleiste_online_offline);
	  		TextView tx_uhrzeit = (TextView) vv.findViewById(R.id.tx_chatleiste_uhrzeit);
	  		ImageView img_abgeschickt = (ImageView) vv.findViewById(R.id.img_chatleiste_abgeschickt);
	  		TextView tx_nachrichtenvorschau = (TextView) vv.findViewById(R.id.tx_chatleiste_nachrichtvorschau);
	  		
	  		if(i==3){
	  			img_bild.setImageResource(R.drawable.profilbild_justin);
	  			tx_benutzername.setText("Peter");
	  			tx_uhrzeit.setText("13:25");
	  			img_abgeschickt.setImageResource(R.drawable.pfeiltest2);
	  			tx_nachrichtenvorschau.setText("Hi wie gehts alter alles klar bei dir ?");
	  			tx_online_offline.setText("online");
	  			img_online_offline.setImageResource(R.drawable.online);
	  		}
	  		if(i==2){
	  			img_bild.setImageResource(R.drawable.profilbild_therock);
	  			tx_benutzername.setText("Hans Meier");
	  			tx_uhrzeit.setText("12:26");
	  			img_abgeschickt.setImageResource(R.drawable.zwei_haeckchen_grau);
	  			tx_nachrichtenvorschau.setText("Jo bis dann! Ciao");
	  			tx_online_offline.setText("offline");
	  			img_online_offline.setImageResource(R.drawable.offline);
	  		}
	  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));
	  		i--;
	  	}
	  	}
	  	else{
	  	if(wen.substring(0,1).matches("p")){
	  		View vv = inflater.inflate(R.layout.chatleiste, null);
	  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_chatleiste_profilbild);
	  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_chatleiste_benutzername);
	  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_chatleiste_online_offline);
	  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_chatleiste_online_offline);
	  		TextView tx_uhrzeit = (TextView) vv.findViewById(R.id.tx_chatleiste_uhrzeit);
	  		ImageView img_abgeschickt = (ImageView) vv.findViewById(R.id.img_chatleiste_abgeschickt);
	  		TextView tx_nachrichtenvorschau = (TextView) vv.findViewById(R.id.tx_chatleiste_nachrichtvorschau);
	  		
	  		img_bild.setImageResource(R.drawable.profilbild_justin);
  			tx_benutzername.setText("Peter");
  			tx_uhrzeit.setText("13:25");
  			img_abgeschickt.setImageResource(R.drawable.pfeiltest2);
  			tx_nachrichtenvorschau.setText("Hi wie gehts alter alles klar bei dir ?");
  			tx_online_offline.setText("online");
  			img_online_offline.setImageResource(R.drawable.online);
		  		
		  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));	
	  	}
	  	if(wen.substring(0,1).matches("h")){
	  		
	  		View vv = inflater.inflate(R.layout.chatleiste, null);
	  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_chatleiste_profilbild);
	  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_chatleiste_benutzername);
	  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_chatleiste_online_offline);
	  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_chatleiste_online_offline);
	  		TextView tx_uhrzeit = (TextView) vv.findViewById(R.id.tx_chatleiste_uhrzeit);
	  		ImageView img_abgeschickt = (ImageView) vv.findViewById(R.id.img_chatleiste_abgeschickt);
	  		TextView tx_nachrichtenvorschau = (TextView) vv.findViewById(R.id.tx_chatleiste_nachrichtvorschau);
	  		
	  		img_bild.setImageResource(R.drawable.profilbild_therock);
  			tx_benutzername.setText("Hans Meier");
  			tx_uhrzeit.setText("12:26");
  			img_abgeschickt.setImageResource(R.drawable.zwei_haeckchen_grau);
  			tx_nachrichtenvorschau.setText("Jo bis dann! Ciao");
  			tx_online_offline.setText("offline");
  			img_online_offline.setImageResource(R.drawable.offline);
  			
	  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));
	  	}
	  	}
    }

    @Override
	public void onBackPressed() {
    	if(gewaehlt == 0){
    		super.onBackPressed();	
    	}
    	if(gewaehlt == 1){
    		sucheschliessen();
    	}
	}

	public void chat_oeffnen(final View view){
    	final View akt_view = view;
    	ImageView img_profilbild = (ImageView) akt_view.findViewById(R.id.img_chatleiste_profilbild);
  		TextView tx_online_offline = (TextView) akt_view.findViewById(R.id.tx_chatleiste_online_offline);
    	
    	String online_offline = "offline";
    	
    	if(tx_online_offline.getText().toString().equalsIgnoreCase("offline")){
    		online_offline = "offline";
    	}
    	else if(tx_online_offline.getText().toString().equalsIgnoreCase("online")){
    		online_offline = "online";
    	}
    		
    	
    	TextView tx_uhrzeit = (TextView) akt_view.findViewById(R.id.tx_chatleiste_uhrzeit);
    		String uhrzeit = tx_uhrzeit.getText().toString();
    	
    	TextView tx_benutzername = (TextView) akt_view.findViewById(R.id.tx_chatleiste_benutzername);
    		String benutzername = tx_benutzername.getText().toString();
    	
    	Intent intent = new Intent(this, Chat.class);
		//intent.putExtra("bild", benutzername);
		intent.putExtra("online_offline", online_offline);
		intent.putExtra("benutzername", benutzername);
		startActivity(intent);
    

//        mConnector = new Connector();
    }
    
    public void chat_suchen(final View view){
    	flipper.setInAnimation(animation.inFromRightAnimation());
     	flipper.setOutAnimation(animation.outToLeftAnimation());
     	flipper.showNext();
     	gewaehlt = 1;
	}
    
    public void suche_schliessen(final View view){
    	sucheschliessen();	
    }
    
    public void sucheschliessen(){
    	flipper.setInAnimation(animation.inFromLeftAnimation());
     	flipper.setOutAnimation(animation.outToRightAnimation());
     	flipper.showPrevious(); 
     	gewaehlt = 0;
    }	
    	public void kontakte_oeffnen(final View view){
    		kontakteoeffnen();
    	}
    	
    	public void kontakteoeffnen(){
    		Intent intent = new Intent(this, Chat_starten.class);
    		startActivity(intent);
    	}
    	
    	public void statusoeffnen(){
    		Intent intent = new Intent(this, Status.class);
    		startActivity(intent);
    	}
    	
    	public void gruppenchatoeffnen(){
    		Intent intent = new Intent(this, Gruppenchat.class);
    		startActivity(intent);
    	}
    	
    	public void freundeinladen(){
    		Intent intent = new Intent(this, Freund_einladen.class);
    		startActivity(intent);
    	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_startseite, menu);
        return true;
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	
		switch (item.getItemId()){
		case R.id.menu_chatten:
			kontakteoeffnen();
			break;
		case R.id.menu_gruppenchat:
			gruppenchatoeffnen();
			break;
		case R.id.menu_mein_status:
//			statusoeffnen();

			break;
		case R.id.menu_einstellungen:
			//kontakteoeffnen();
			break;
			
		default:
			break;
			
		}
		return super.onOptionsItemSelected(item);
	}
}
